﻿Imports DAL.DAL

Public Class DAL_BackUpRestore

    Public Shared Sub restore(ByVal p1Ruta As String, ByVal p2CantidadArchivos As Integer)

        Dim str As String

        If p1Ruta = "" Then
            Throw New ApplicationException("La ruta no puede estar vacia.")
        End If

        If p2CantidadArchivos = 0 Then
            Throw New ApplicationException("La cantidad de archivos debe ser mayor a 0.")
        End If

        Select Case p2CantidadArchivos
            Case 2
                Dim origen1 As String
                Dim origen2 As String

                origen1 = p1Ruta.ToString & "\backup1.bak"
                origen2 = p1Ruta.ToString & "\backup2.bak"
                str = String.Format("use master;ALTER DATABASE Estudio SET SINGLE_USER WITH ROLLBACK IMMEDIATE; RESTORE DATABASE Estudio FROM DISK = N'{0}', DISK = N'{1}' WITH REPLACE; ALTER DATABASE Estudio SET MULTI_USER", origen1, origen2)
            Case 3
                Dim origen1 As String
                Dim origen2 As String
                Dim origen3 As String

                origen1 = p1Ruta.ToString & "\backup1.bak"
                origen2 = p1Ruta.ToString & "\backup2.bak"
                origen3 = p1Ruta.ToString & "\backup3.bak"
                str = String.Format("use master;ALTER DATABASE Estudio SET SINGLE_USER WITH ROLLBACK IMMEDIATE; RESTORE DATABASE Estudio FROM DISK = N'{0}', DISK = N'{1}' , DISK = N'{2}' WITH REPLACE; ALTER DATABASE Estudio SET MULTI_USER", origen1, origen2, origen3)
            Case 4
                Dim origen1 As String
                Dim origen2 As String
                Dim origen3 As String
                Dim origen4 As String

                origen1 = p1Ruta.ToString & "\backup1.bak"
                origen2 = p1Ruta.ToString & "\backup2.bak"
                origen3 = p1Ruta.ToString & "\backup3.bak"
                origen4 = p1Ruta.ToString & "\backup4.bak"
                str = String.Format("use master;ALTER DATABASE Estudio SET SINGLE_USER WITH ROLLBACK IMMEDIATE; RESTORE DATABASE Estudio FROM DISK = N'{0}', DISK = N'{1}' , DISK = N'{2}' , DISK = N'{3}' WITH REPLACE; ALTER DATABASE Estudio SET MULTI_USER", origen1, origen2, origen3, origen4)
            Case Else
                Dim origen As String

                origen = p1Ruta.ToString & "\backup.bak"
                str = String.Format("use master;ALTER DATABASE Estudio SET SINGLE_USER WITH ROLLBACK IMMEDIATE; RESTORE DATABASE Estudio FROM DISK = N'{0}' WITH REPLACE; ALTER DATABASE Estudio SET MULTI_USER", origen)

        End Select

        Try
            Helper_DAL.ExecuteNonQuery(str)
        Catch ex As Exception
            Throw ex
        End Try


    End Sub

    Public Shared Sub backup(ByVal p1ruta As String, ByVal p2cantidadArchivos As Integer)

        Dim str As String
        'Dim mDia As Date = DateTime.Today


        If p1ruta = "" Then
            Throw New ApplicationException("La ruta no puede estar vacia.")
        End If

        If p2cantidadArchivos = 0 Then
            Throw New ApplicationException("La cantidad de archivos debe ser mayor a 0.")
        End If

        Select Case p2cantidadArchivos
            Case 2

                Dim destino1 As String
                Dim destino2 As String
                destino1 = p1ruta.ToString() & "\backup1.bak"
                destino2 = p1ruta.ToString() & "\backup2.bak"
                str = String.Format("BACKUP DATABASE Estudio TO DISK = '{0}', DISK = '{1}' WITH DESCRIPTION =N'Nada', NOFORMAT, NOINIT, NAME=N'Nombre1', SKIP, NOREWIND, NOUNLOAD, STATS = 10", destino1, destino2)

            Case 3

                Dim destino1 As String
                Dim destino2 As String
                Dim destino3 As String
                destino1 = p1ruta.ToString() & "\backup1.bak"
                destino2 = p1ruta.ToString() & "\backup2.bak"
                destino3 = p1ruta.ToString() & "\backup3.bak"
                str = String.Format("BACKUP DATABASE Estudio TO DISK = '{0}', DISK = '{1}' , DISK = '{2}' WITH DESCRIPTION =N'Nada', NOFORMAT, NOINIT, NAME=N'Nombre1', SKIP, NOREWIND, NOUNLOAD, STATS = 10", destino1, destino2, destino3)

            Case 4

                Dim destino1 As String
                Dim destino2 As String
                Dim destino3 As String
                Dim destino4 As String
                destino1 = p1ruta.ToString() & "\backup1.bak"
                destino2 = p1ruta.ToString() & "\backup2.bak"
                destino3 = p1ruta.ToString() & "\backup3.bak"
                destino4 = p1ruta.ToString() & "\backup4.bak"
                str = String.Format("BACKUP DATABASE Estudio TO DISK = '{0}', DISK = '{1}', DISK = '{2}' , DISK = '{3}' WITH DESCRIPTION =N'Nada', NOFORMAT, NOINIT, NAME=N'Nombre1', SKIP, NOREWIND, NOUNLOAD, STATS = 10", destino1, destino2, destino3, destino4)

            Case Else

                Dim destino As String
                destino = p1ruta.ToString() & "\backup.bak"
                str = String.Format("BACKUP DATABASE Estudio TO DISK = '{0}' WITH DESCRIPTION =N'Nada', NOFORMAT, NOINIT, NAME=N'Nombre1', SKIP, NOREWIND, NOUNLOAD, STATS = 10", destino)

        End Select


        Try
            Helper_DAL.ExecuteNonQuery(str)
        Catch ex As Exception
            Throw ex
        End Try


    End Sub

End Class
